package com.epcs.controller.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.epcs.model.bo.RoleBO;
import com.epcs.model.bo.UserBO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Email;
import com.epcs.model.dto.Role;
import com.epcs.model.dto.Setting;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.jersey.api.representation.Form;

public class RegistryAction extends ActionSupport implements SessionAware, ServletRequestAware {

	// ---------- input fields ----------//
	private String username;
	private String email;
	private String password;
	private String fullname;
	private String lastupdated;
	private Role role;
	private Boolean isActive;
	private Set<Setting> settings = new HashSet<Setting>(0);

	// ---------- input fields ----------//
	private SessionMap<String, Object> sessionMap;
	private HttpServletRequest request;

	// ----------Action method -----------//
	public String register() {
		String resultString = "Fail";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		format.setTimeZone(TimeZone.getTimeZone("Asia/Ho_Chi_Minh"));
		Calendar calendar = Calendar.getInstance();

		lastupdated = null;
		role = new RoleBO().getRoleByName("Member");
		isActive = true;

		Account account = new Account(username, role, password, lastupdated, fullname, isActive, null, settings, null,
				null, null);
		Setting setting = new Setting(account, true, 30);
		settings.add(setting);

		boolean result = new UserBO().register(account);
		if (result) {
			resultString = "Success";
//			//login luôn
//			Account newUser = (Account) sessionMap.get("USER_INFO");
//			if (newUser == null){
//				newUser = new UserBO().checkLogin(username, password);
//				sessionMap.put("USER_INFO", newUser);
//			}
			request.setAttribute("SUCCESSFUL_MESSAGE", "Đăng ký tài khoản thành công!");
			sessionMap.put("EMAIL_PERMISSION", username);
			
		} else {
			request.setAttribute("ON_SCREEN", "RegisterPage");
			request.setAttribute("REGISTRY_MESSAGE", "Tên đăng nhập này đã được sử dụng!");
		}

		return resultString;
	}

	// -----------Getter and Setter -----//
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = (SessionMap<String, Object>) sessionMap;
		
	}

}
